package com.guigu.leizihao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.SCellMapper;
import com.guigu.common.mapper.SGatherDetailsMapper;
import com.guigu.common.mapper.SGatherMapper;
import com.guigu.common.pojo.*;
import com.guigu.common.util.GetUUidUtil;
import com.guigu.leizihao.vo.SGatherDetailsView;
import com.guigu.leizihao.vo.SGatherVo;
import com.guigu.common.vo.SearchVo;
import com.guigu.leizihao.service.SGatherService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SGatherServiceImpl implements SGatherService {

    @Resource
    public SGatherMapper sGatherMapper;

    @Resource
    public SGatherDetailsMapper sGatherDetailsMapper;

    @Resource
    public SCellMapper scellMapper;

    @Resource
    private GetUUidUtil getUUidUtil;

    /**
     * 查询已分配配置单的产品
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo<DFile> findDfileInDESIGN_CELL_TAG(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());

        PageInfo p=new PageInfo(this.sGatherMapper.findDfileInDESIGN_CELL_TAG(searchVo));
        searchVo.setRows(p.getList());
        searchVo.setTotal(p.getTotal());
        return searchVo;
    }

    /**
     * 添加入库申请，和明细
     * @param sGatherVo
     */
    @Override
    public void addGather(SGatherVo sGatherVo) {
        String s = getUUidUtil.returnId("401");
        SGather sgather = sGatherVo.getSgather();
        sgather.setGatherId(s);
        sgather.setCheckTag("s001-0");
        this.sGatherMapper.addGather(sgather);
        Integer id = sgather.getId();
        List<SGatherDetails> list = sGatherVo.getList();
        addGatherDetail(list,id);
    }

    /**
     * 查询待审核的入库申请
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo queryGatherToAudit(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sGatherMapper.queryGatherToAudit(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    /**
     * 跟据parentId查询入库申请明细
     * @param id
     * @return
     */
    @Override
    public List<SGatherDetails> findGatherDetailsById(int id) {
       return this.sGatherDetailsMapper.findGatherDetailsById(id);

    }

    @Override
    public int changeSGather(SGather sGather) {
        this.changeSGatherToAudit(sGather.getId());
        return this.sGatherMapper.changeSGather(sGather);
    }

    @Override
    public int changeSGatherNo(Integer id) {
        SGather s=new SGather();
        s.setId(id);
       SysUsers u= (SysUsers) SecurityUtils.getSubject().getSession().getAttribute("user");
       s.setChecker(u.getLoginId());
        return this.sGatherMapper.changeSGatherNo(s);
    }

    @Override
    public SearchVo queryGatherAll(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sGatherMapper.queryGatherAll(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());


        return searchVo;
    }

    @Override
    public SearchVo findFormulateGather(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sGatherMapper.findFormulateGather(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());


        return searchVo;
    }

    /**
     * 根据产品编号查询对应的安全配置单
     * @param productid
     * @return
     */
    @Override
    public SCell queryCellByProductId(String productid) {
        return  this.sGatherMapper.queryCellByProductId(productid);

    }

    @Override
    public int UpdateGather_details(Integer id) {

        return this.sGatherDetailsMapper.UpdateGather_details(id);
    }

    @Override
    public SearchVo<SGather> queryGather(SearchVo<SGather> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sGatherMapper.queryGatherSelective(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    @Override
    public void updateGather_detailsquantityInStorage(SGatherVo sGatherVo) {
        this.sGatherDetailsMapper.updateGather_detailsquantityInStorage(sGatherVo.getList());
    }

    @Override
    public SearchVo queryEnterbankCheck(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sGatherMapper.queryEnterbankCheck(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;

    }

    @Override
    public List<SGatherDetails> findGatherDetailsByIdandQuantityIinStorage(Integer id) {

     return    this.sGatherDetailsMapper.findGatherDetailsByIdandQuantityIinStorage(id);
    }

    @Override
    public void EnterbankCheckPass(SGatherVo<SGatherDetails> sGatherVo) {
        this.sGatherDetailsMapper.EnterbankCheckPass(sGatherVo.getList());
        this.scellMapper.updateamountByproduct_id(sGatherVo.getList());
        SGather sgather = sGatherVo.getSgather();
        int sum=0;
        for(SGatherDetails s:sGatherVo.getList()){
            sum+=s.getQuantityInStorage();
        }
        sgather.setGatheredAmountSum(sum);
        System.out.println(sgather);
        //查询入库单是否已登记完
        this.sGatherMapper.updategathered_amount_sum(sgather);
      int i=  this.sGatherDetailsMapper.queryAmountById(sGatherVo.getList().get(0).getId());
        System.out.println(i);
        if(i==0){
                //更改入库单状态未已调度

                    sgather.setAttemper(((SysUsers)SecurityUtils.getSubject().getSession().getAttribute("user")).getLoginId());
                this.sGatherMapper.updatestore_tagByGatherId(sgather);
        }

    }

    @Override
    public Integer queryTotalcost(Integer id) {
        return this.sGatherDetailsMapper.queryTotalcost(id);
    }

    /**
     * 根据产品编号查询所有的明细
     * @param productid
     * @return
     */
    @Override
    public List<SGatherDetailsView> queryGatherDetailsByproductId(String productid) {
        return this.sGatherDetailsMapper.queryGatherDetailsByproductId(productid);
    }

    /**
     * 跟据parent_id更改入库申请明细的登记标志为已登记
     * @param id
     */
    public void changeSGatherToAudit(int id){

            this.sGatherDetailsMapper.changeSGatherToAudit(id);

    }



    /**
     * 添加入库明细
     * @param list
     * @param id
     */
    public void addGatherDetail(List<SGatherDetails> list,Integer id){
        System.out.println(id);
        for (SGatherDetails d:list){
            d.setParentId(id);
        }
        this.sGatherDetailsMapper.addGatherDetail(list);
    }
}
